package edu.xja.controller;

import com.github.pagehelper.PageInfo;
import edu.xja.entity.Academy;
import edu.xja.entity.Admin;
import edu.xja.service.AcademyService;
import edu.xja.service.impl.AcademyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;

@WebServlet("/admin/academy/*")
public class AcademyController extends BaseController {
    private AcademyService academyService=new AcademyServiceImpl();

    public String addPage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        return "/jsp/admin/academy/academy_add.jsp";
    }

    public String addSave(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //获取前端信息
        String academyName=request.getParameter("academyName");
        Academy academy=new Academy();
        academy.setAcademyName(academyName);
        LocalDateTime localDateTime=LocalDateTime.now();
        academy.setCreateTime(localDateTime);
        Admin admin= (Admin) request.getSession().getAttribute("login_admin");
        academy.setCreateBy(admin.getAdminName());
        academy.setLastUpdateTime(localDateTime);
        academy.setLastUpdateBy(admin.getAdminName());
        int result=academyService.add(academy);
        request.setAttribute("tip_info","添加学院成功！");
        return "/result.jsp";
    }
    public String updatePage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String strId=request.getParameter("id");
        Academy academy=academyService.getById(Integer.valueOf(strId));
        request.setAttribute("academy_info",academy);
        return "/jsp/admin/academy/academy_edit.jsp";
    }
    public String updateSave(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String academyName=request.getParameter("academyName");
        String academyId=request.getParameter("id");
        Academy academy =new Academy();
        academy.setAcademyName(academyName);
        academy.setId(Integer.parseInt(academyId));
        academy.setLastUpdateTime(LocalDateTime.now());
        Admin admin= (Admin) request.getSession().getAttribute("login_admin");
        academy.setLastUpdateBy(admin.getAdminName());
        int result=academyService.updateById(academy);
        if(result==1) {
            request.setAttribute("tip_info", "修改学院成功！");
        }
        return "/result.jsp";
    }
    public String delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String id=request.getParameter("id");
        Academy academy=new Academy();
        academy.setId(Integer.parseInt(id));
        academy.setLastUpdateTime(LocalDateTime.now());
        Admin admin= (Admin) request.getSession().getAttribute("login_admin");
        academy.setLastUpdateBy(admin.getAdminName());
        int result=academyService.delete(academy);
        request.setAttribute("tip_info","删除学院成功！");
        return "search?pageNum=1";
    }
    public String search(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int pageNum = Integer.valueOf(request.getParameter("pageNum"));
        PageInfo<Academy> academyPageInfo = academyService.search(pageNum, PAGE_SIZE);
        request.setAttribute("page_vo", academyPageInfo);
        return "/jsp/admin/academy/academy_list.jsp";
    }
    
}
